package cc.cc4414.cc02.system.controller;

import java.util.List;

import javax.validation.constraints.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cc.cc4414.cc02.core.aspect.LogAspect;
import cc.cc4414.cc02.core.constant.RegexpConsts;
import cc.cc4414.cc02.core.controller.BaseController;
import cc.cc4414.cc02.core.result.ResultEnum;
import cc.cc4414.cc02.core.result.ResultVO;
import cc.cc4414.cc02.system.entity.Authority;
import cc.cc4414.cc02.system.entity.Role;
import cc.cc4414.cc02.system.form.RoleAuthorityForm;
import cc.cc4414.cc02.system.service.RoleService;

/**
 * @author cc 2018年7月15日
 */
@Validated
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;

	/**
	 * 查询所有角色信息
	 * 
	 * @return 所有角色
	 */
	@GetMapping("all")
	public ResultVO<List<Role>> all() {
		return new ResultVO<List<Role>>(roleService.findAll());
	}

	/**
	 * 通过角色id查找能访问该资源的所有权限
	 * 
	 * @param roleId
	 *            角色id
	 * @return 该角色能访问该资源的所有权限
	 */
	@GetMapping("findAuthority")
	public ResultVO<List<Authority>> findAuthority(@RequestParam Long roleId) {
		return new ResultVO<List<Authority>>(roleService.findAuthorityByRoleId(roleId));
	}

	/**
	 * 新增角色
	 * 
	 * @param name
	 *            角色名称
	 * @param description
	 *            描述
	 * @return
	 */
	@PostMapping("add")
	@LogAspect("新增角色")
	public ResultVO<Object> add(@Pattern(regexp = RegexpConsts.ROLE_NAME) @RequestParam String name,
			@Pattern(regexp = RegexpConsts.ROLE_DESCRIPTION) @RequestParam String description) {
		Role role = new Role();
		role.setName(name);
		role.setDescription(description);
		roleService.save(role);
		return new ResultVO<>(ResultEnum.SUCCESS);
	}

	/**
	 * 检查角色是否存在
	 * 
	 * @param name
	 *            角色名
	 * @return 角色存在为true，角色不存在为false
	 */
	@GetMapping("checkRole")
	public ResultVO<Boolean> checkRole(@Pattern(regexp = RegexpConsts.ROLE_NAME) @RequestParam String name) {
		ResultVO<Boolean> resultVO = new ResultVO<Boolean>(ResultEnum.SUCCESS);
		Role role = roleService.findByName(name);
		if (role != null) {
			resultVO.setData(true);
		} else {
			resultVO.setData(false);
		}
		return resultVO;
	}

	/**
	 * 修改描述
	 * 
	 * @param id
	 *            角色id
	 * @param description
	 *            修改后的描述
	 * @return 是否修改成功
	 */
	@PostMapping("updateDescription")
	@LogAspect("修改角色描述")
	public ResultVO<Object> updateDescription(@RequestParam Long id,
			@Pattern(regexp = RegexpConsts.ROLE_DESCRIPTION) @RequestParam String description) {
		roleService.updateDescription(id, description);
		return new ResultVO<>(ResultEnum.SUCCESS);
	}

	/**
	 * 通过角色id删除角色
	 * 
	 * @param id
	 *            角色id
	 * @return 是否删除成功
	 */
	@PostMapping("delete")
	@LogAspect("删除角色")
	public ResultVO<Object> delete(@RequestParam Long id) {
		roleService.delete(id);
		return new ResultVO<>(ResultEnum.SUCCESS);
	}

	/**
	 * 修改角色所拥有的所有权限
	 * 
	 * @param roleAuthority
	 *            角色id和角色所拥有的权限id
	 * @return 是否修改成功
	 */
	@PostMapping("updateRoleAuthority")
	@LogAspect("修改角色权限")
	public ResultVO<Object> updateRoleAuthority(@Validated @RequestBody RoleAuthorityForm roleAuthority) {
		roleService.updateRoleAuthority(roleAuthority.getRoleId(), roleAuthority.getAuthorityIds());
		return new ResultVO<>(ResultEnum.SUCCESS);
	}
}
